/**
 * 
 */
package fr.univ_tln.inf9.exaplanning.gui.utilisateur;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import fr.univ_tln.inf9.exaplanning.controleur.utilisateur.UserController;
import fr.univ_tln.inf9.exaplanning.controleur.utilisateur.UserCreateView;

/**
 * @author pivi
 *
 */
public class JFrameCreateTeacher extends UserCreateView implements ActionListener {
	
	
	/**
	 * Variables utiles pour le modèle
	 */
	private String mail = null;
	private String name = null;
	private String first_name = null;
	
	/**
	 * NOUVEAU: une instance du controleur est accessible
	 * Le controlleur
	 */
	private UserController ctrl;
	
	/**
	 * Flag
	 */
	
	private Boolean open;
	/**
	 * Variables pour la gui
	 */
	private JFrame frame = null;
	private JPanel contentPane = null;
	private JLabel mailLabel = null;
	private JTextField mailField = null;
	private JLabel nameLabel = null;
	private JTextField nameField = null;
	private JLabel first_nameLabel = null;
	private JTextField first_nameField = null;
	
	private JButton okButton;
	

	
	public JFrameCreateTeacher(UserController controller){
		super(controller);
		this.ctrl = controller;
		buildFrame();
	}
	
	private void buildFrame() {
		frame = new JFrame();
		
		contentPane = new JPanel();
		//this.contentPane.setSize(20,30);
		
	
		okButton = new JButton("OK");
		okButton.addActionListener(this);
		
		mailLabel = new JLabel("Entrer mail : ");
		contentPane.add(mailLabel);
		mailField = new JTextField("        ");
		mailField.setSize(new Dimension(50,10));
		contentPane.add(mailField);
		nameLabel = new JLabel("Entrer nom : ");
		contentPane.add(nameLabel);
		nameField = new JTextField("        ");
		nameField.setSize(new Dimension(50,10));
		contentPane.add(nameField);
		first_nameLabel = new JLabel("Entrer prenom : ");
		contentPane.add(first_nameLabel);
		first_nameField = new JTextField("        ");
		first_nameField.setSize(new Dimension(50,10));
		contentPane.add(first_nameField);
		
		
		contentPane.add(okButton);
		
		
		frame.setContentPane(contentPane);
		frame.setTitle("Création Professeur");
		frame.pack();
	}

	/**
	 * Lorsque on clique sur ok, on appelle la nouvelle méthode
	 * createStudent du controleur qui se charge de créer un étudiant
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand().equals("OK")){
			mail = (String)mailField.getText();
			name = (String)nameField.getText();
			first_name = (String)first_nameField.getText();
			try {
				ctrl.createTeacher(mail, first_name, name);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			frame.dispose();
			
		}
	}
	
	
	
	
	
	/**
	 * @return the mail
	 */
	public String getMail() {
		return mail;
	}

	/**
	 * @param mail the mail to set
	 */
	public void setMail(String mail) {
		this.mail = mail;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the first_name
	 */
	public String getFirst_name() {
		return first_name;
	}

	/**
	 * @return the open
	 */
	public Boolean getOpen() {
		return open;
	}

	/**
	 * @param first_name the first_name to set
	 */
	public void setFirst_name(String first_name) {
		this.first_name = first_name;
	}

	public void display() {
		frame.setVisible(true);
		open = true;
	}

	public void close() {
		frame.dispose();
		open = false;
	}

}
